/**
 * @Author: wilmerxu
 * @Description: 消息通信
 * @Date: 2020/7/28 11:50
 */
const CopyWebpackPlugin = require('copy-webpack-plugin');
const path = require('path');

// Generate pages object
const pagesObj = {};

const chromeName = ['popup', 'options'];

chromeName.forEach(name => {
  pagesObj[name] = {
    entry: `src/${name}/index.js`,
    template: 'public/index.html',
    filename: `${name}.html`,
  };
});

const plugins =
  process.env.NODE_ENV === 'production'
    ? [
      {
        from: path.resolve('src/manifest.production.json'),
        to: `${path.resolve('dist')}/manifest.json`,
      },
    ]
    : [
      {
        from: path.resolve('src/manifest.development.json'),
        to: `${path.resolve('dist')}/manifest.json`,
      },
      {
        from: path.resolve('src/icons/icon_48.png'),
        to: `${path.resolve('dist')}/icons/icon_48.png`,
      },
      {
        from: path.resolve('src/icons/icon_128.png'),
        to: `${path.resolve('dist')}/icons/icon_128.png`,
      },
    ];

module.exports = {
  pages: pagesObj,
  configureWebpack: {
    entry: {
      'content': './src/content/index.js',
      'background': './src/background/background.js'
    },
    output: {
      filename: 'js/[name].js',
    },
    plugins: [CopyWebpackPlugin(plugins)],
    devtool: 'source-map'
  },
  css: {
    extract: {
      filename: 'css/[name].css',
      // chunkFilename: 'css/[name].css'
    },
  },
  productionSourceMap: true,
};
